/* File 2 of 2 for Appendix Figures 2 and 3 */




#delimit ;
clear all;

local outfile "GWgapr10_pfbyind_TL_plot";
set more off;


di _n "$S_DATE $S_TIME";










********************************************************************************;
* preparing the data;
********************************************************************************;

local lab fte_del; 

use GWgapr10_pfbyind_TL_res_`lab', clear;


* setting any coeff with a very small se or missing se to missing;

foreach eq in pf wb {; * y w;
	foreach var in lnM lnK lnL phi_f phi_lt25 phi_40to54 phi_55p delta {;
		replace c_`var'_`eq' = . if abs(se_`var'_`eq')<0.00001
			| se_`var'_`eq'>=.;
		replace se_`var'_`eq' = . if abs(se_`var'_`eq')<0.00001
			| se_`var'_`eq'>=.;
	};
};


* making phis positive and making discrimination estimate variables;

foreach phi in phi_f phi_lt25 phi_40to54 phi_55p {;

	foreach eq in wb pf {;
		replace c_`phi'_`eq' = abs(c_`phi'_`eq'); 
	};
	
	gen discrim_`phi' = 1 - (c_`phi'_wb/c_`phi'_pf);
	
};

replace discrim_phi_f = . if se_discrim<0.00001 | se_discrim==.;
replace se_discrim = . if se_discrim<0.00001 | se_discrim==.;


order ind3 obs *discrim*;



label var ind3 "Level 3 productivity industry";
label var obs "Number of observations";
notes: c_ variables are coefficients, se_ variables are standard errors.;

compress;
save `outfile'_`lab', replace;


/*
* making confidentialised version of data that can go out of datalab;

grr obs;
compress;
drop obs;
label var rr3obs "Number of observations, randomly rdd to base 3";
order ind3 rr3obs;

notes: `outfile'_`lab'_conf.dta is the version of `outfile'.dta that has
observation numbers randomly rounded to base 3.;
save `outfile'_`lab'_conf, replace;
*/



********************************************************************************;
* Plots;
********************************************************************************;


**************************;
* Appendix Figure 3: rcap plot by industry: only industries with not-too-huge standard errors;

use `outfile'_`lab', clear;

/* This is commented out only to make the fake code fun ;
keep if se_discrim<0.5; *drops 7 industries, and one didn't converge;
drop if discrim_phi_f>=. | discrim_phi_f<-1;
*/

local var discrim_phi_f;
sort `var';
gen i = _n;
gen `var'_min = `var' - 1.96*se_discrim;
gen `var'_max = `var' + 1.96*se_discrim;


local i_lab "";
sum obs, mean;
local obs = r(N);

forvalues i = 1/`obs' {;
	local ind = ind3[`i'];
	local i_lab `" `i_lab' `i' `ind' "';
};
di "`i_lab'";
label define i_lab `i_lab';
label val i i_lab;

qui sum se_discrim;
local inobs = r(N);

graph twoway
	(rcap `var'_min `var'_max i, lc(blue))
	(scatter `var' i, msize(vsmall) mc(black)),
	ytitle("Point Estimate and 95% CI for" "Gender Wage-Productivity Gap")
	xtitle("Industry")
	legend(off)
	xlabel(1/`inobs', valuelabel angle(vertical) labs(*.9))
	xmtick(#`inobs') ylab(-1.4(0.2)1.2) ytick(-1.2(0.2)1.2) yscale(range(-1.2 1.2))
	yline(0, lc(gs5) lp(shortdash))
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white));
	
graph export "`outfile'_`lab'_rcap_`var'.pdf", replace;





**************************;
* Appendix Figure 2: scatter plots of phis from pf and wb equations;

use `outfile'_`lab', clear;

local var f;

local samp "c_phi_`var'_pf>0 & c_phi_`var'_pf<2
			& c_phi_`var'_wb>0 & c_phi_`var'_wb<2";
			
count if `samp';
count if !(`samp');

gen diagx = 0;
gen diagy = 0;
replace diagx = 1.5 if _n==1;
replace diagy = 1.5 if _n==1;

foreach var in f {;

	sum c_phi_`var'_pf;
	
	graph twoway
		(scatter c_phi_`var'_pf c_phi_`var'_wb if `samp', mc(blue) msize(small))
		(scatter diagy diagx, mstyle(none) c(l) lc(gs3) lp(shortdash) lw(*.8)),
		legend(off)
		xline(1, lc(gs9) lp(dash))
		yline(1, lc(gs9) lp(dash))
		xtitle("Phi_`var' in the wage bill equation")
		ytitle("Phi_`var' in the production equation")
		graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white))
		xscale(range(0 1.5)) yscale(range(0 1.5))
		xlabel(0(0.5)1.5) ylabel(0(0.5)1.5);
		
	graph export "`outfile'_`lab'_scat_`var'.pdf", replace;
	
	
};
